package com.gxuwz.weather.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class CityDao {
    private CityDatabaseHelper dbHelper;
    private SQLiteDatabase database;

    public CityDao(Context context) {
        dbHelper = new CityDatabaseHelper(context);
    }

    public void open() {
        database = dbHelper.getWritableDatabase();
    }

    public void close() {
        dbHelper.close();
    }

    public long insertCity(String cityName) {
        ContentValues values = new ContentValues();
        values.put(CityDatabaseHelper.COLUMN_CITY_NAME, cityName);
        values.put(CityDatabaseHelper.COLUMN_ADD_TIME, System.currentTimeMillis());
        return database.insert(CityDatabaseHelper.TABLE_CITIES, null, values);
    }

    public int deleteCity(String cityName) {
        return database.delete(CityDatabaseHelper.TABLE_CITIES,
                CityDatabaseHelper.COLUMN_CITY_NAME + " = ?",
                new String[]{cityName});
    }

    public List<String> getAllCities() {
        List<String> cities = new ArrayList<>();
        Cursor cursor = database.query(CityDatabaseHelper.TABLE_CITIES,
                new String[]{CityDatabaseHelper.COLUMN_CITY_NAME},
                null, null, null, null,
                CityDatabaseHelper.COLUMN_ADD_TIME + " ASC");

        if (cursor != null) {
            while (cursor.moveToNext()) {
                String cityName = cursor.getString(cursor.getColumnIndexOrThrow(CityDatabaseHelper.COLUMN_CITY_NAME));
                cities.add(cityName);
            }
            cursor.close();
        }
        return cities;
    }

    public boolean isCityExists(String cityName) {
        Cursor cursor = database.query(CityDatabaseHelper.TABLE_CITIES,
                new String[]{CityDatabaseHelper.COLUMN_CITY_NAME},
                CityDatabaseHelper.COLUMN_CITY_NAME + " = ?",
                new String[]{cityName},
                null, null, null);
        boolean exists = cursor != null && cursor.getCount() > 0;
        if (cursor != null) {
            cursor.close();
        }
        return exists;
    }

    /**
     * 模糊查询所有城市表（all_cities），支持areaCn和areaCode字段
     * @param keyword 查询关键字
     * @return 匹配的CityInfo列表
     */
    public List<com.gxuwz.weather.model.CityInfo> searchCities(String keyword) {
        List<com.gxuwz.weather.model.CityInfo> result = new ArrayList<>();
        // 搜索areaCn和areaCode字段
        String sql = "SELECT DISTINCT areaCode, areaId, areaEn, areaCn, cityEn, cityCn, provEn, provCn FROM all_cities " +
                "WHERE areaCn LIKE ? OR areaCode LIKE ? " +
                "ORDER BY areaCn " +
                "LIMIT 30";
        String like = "%" + keyword + "%";
        Cursor cursor = database.rawQuery(sql, new String[]{like, like});
        if (cursor != null) {
            while (cursor.moveToNext()) {
                com.gxuwz.weather.model.CityInfo info = new com.gxuwz.weather.model.CityInfo();
                info.setAreaCode(cursor.getString(cursor.getColumnIndexOrThrow("areaCode")));
                info.setAreaId(cursor.getString(cursor.getColumnIndexOrThrow("areaId")));
                info.setAreaEn(cursor.getString(cursor.getColumnIndexOrThrow("areaEn")));
                info.setAreaCn(cursor.getString(cursor.getColumnIndexOrThrow("areaCn")));
                info.setCityEn(cursor.getString(cursor.getColumnIndexOrThrow("cityEn")));
                info.setCityCn(cursor.getString(cursor.getColumnIndexOrThrow("cityCn")));
                info.setProvEn(cursor.getString(cursor.getColumnIndexOrThrow("provEn")));
                info.setProvCn(cursor.getString(cursor.getColumnIndexOrThrow("provCn")));
                result.add(info);
            }
            cursor.close();
        }
        return result;
    }
} 